package org.flowers.unbeaten.security;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @description Shiro配置类
 * @author RyanWang
 * @date 2021-07-16 15:31:30
 * @version 1.0
 */
@Configuration
public class ShiroConfiguration {

    /**
     * 获取自定义哈希凭证匹配器
     * @return
     */
    @Bean
    public MyHashedCredentialsMatcher myHashedCredentialsMatcher() {
        return new MyHashedCredentialsMatcher();
    }

    /**
     * 获取自定义Realm
     * @return
     */
    @Bean
    public MyShiroRealm myShiroRealm() {
        MyShiroRealm myShiroRealm = new MyShiroRealm();
        // 把自定义的认证器设置到自定义的Realm中
        myShiroRealm.setCredentialsMatcher(myHashedCredentialsMatcher());
        return myShiroRealm;
    }

    /**
     * 设置安全管理器
     * @return
     */
    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        // 关联自定义Realm
        securityManager.setRealm(myShiroRealm());
        return securityManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        // 设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        LinkedHashMap<String, Filter> linkedHashMap = new LinkedHashMap<>();
        // 校验token
        linkedHashMap.put("token", new MyAccessControlFilter());
        shiroFilterFactoryBean.setFilters(linkedHashMap);
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        // 放开所有的请求方便研发调试接口 filterChainDefinitionMap.put("/**/**/**", "anon");

        // 配置不会被拦截的链接 顺序判断
        filterChainDefinitionMap.put("/user/user/login", "anon");

        // 放行代码生成器相关接口
        filterChainDefinitionMap.put("/generator/generator/**", "anon");

        // 放行代码生成器相关页面和js、css文件
        filterChainDefinitionMap.put("/view/generator/**", "anon");
        filterChainDefinitionMap.put("/assets/**", "anon");
        filterChainDefinitionMap.put("/component/**", "anon");
        filterChainDefinitionMap.put("/css/**", "anon");
        filterChainDefinitionMap.put("/templates/**", "anon");

        // 放行用户登录相关接口
        filterChainDefinitionMap.put("/user/user/token", "anon");
        filterChainDefinitionMap.put("/user/getVerify", "anon");
        filterChainDefinitionMap.put("/user/checkVerify", "anon");
        filterChainDefinitionMap.put("/index/**", "anon");
        filterChainDefinitionMap.put("*.html", "anon");
        filterChainDefinitionMap.put("/static/**", "anon");
        filterChainDefinitionMap.put("/doc.html", "anon");

        // 放行swagger-ui地址
        filterChainDefinitionMap.put("/swagger/**", "anon");
        filterChainDefinitionMap.put("/v2/api-docs", "anon");
        filterChainDefinitionMap.put("/swagger-ui.html", "anon");
        filterChainDefinitionMap.put("/swagger-resources/**", "anon");
        filterChainDefinitionMap.put("/webjars/**", "anon");
        filterChainDefinitionMap.put("/favicon.ico", "anon");
        filterChainDefinitionMap.put("/captcha.jpg", "anon");

        // 放行druid sql监控配置
        filterChainDefinitionMap.put("/druid/**", "anon");
        filterChainDefinitionMap.put("/csrf", "anon");

        //文件上传可直接访问
//        filterChainDefinitionMap.put(fileUploadProperties.getAccessUrl(), "anon");
        filterChainDefinitionMap.put("/images/**", "anon");
        filterChainDefinitionMap.put("/js/**", "anon");
        filterChainDefinitionMap.put("/layui/**", "anon");
        filterChainDefinitionMap.put("/css/**", "anon");
        filterChainDefinitionMap.put("/layui-ext/**", "anon");
        filterChainDefinitionMap.put("/**", "token,authc");
        shiroFilterFactoryBean.setLoginUrl("/index/login");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }

    /**
     * 开启shiro aop注解支持
     * 使用代理方式
     * 所以需要开启代码支持
     * @param securityManager
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

}
